package com.zhengxing.jienci.content.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhengxing.jienci.common.pojo.CustomizePage;
import com.zhengxing.jienci.common.pojo.Tag;
import com.zhengxing.jienci.common.support.SimpleResponse;
import com.zhengxing.jienci.common.util.CommonUtils;
import com.zhengxing.jienci.common.util.IdGenerateUtil;
import com.zhengxing.jienci.content.mapper.TagMapper;
import com.zhengxing.jienci.content.service.TagService;
import com.zhengxing.jienci.web.context.SecurityContext;

@Service
public class TagServiceImpl implements TagService {
	
	@Autowired
	private TagMapper tagMapper;

	@Override
	public List<Tag> query(Tag tag) {
		QueryWrapper<Tag> queryWrapper = new QueryWrapper<Tag>(tag);
		return tagMapper.selectList(queryWrapper);
	}

	@Override
	public List<Tag> queryByMap(Map<String, Object> params) {
		return tagMapper.selectByMap(params);
	}

	@Override
	public Tag queryById(long id) {
		return tagMapper.selectById(id);
	}
	
	@Override
	public IPage<Tag> pageQuery(CustomizePage page, Tag tag) {
		IPage<Tag> ipage = new Page<Tag>(page.getPageNow(), page.getPageSize());
		QueryWrapper<Tag> queryWrapper = new QueryWrapper<Tag>(tag);
		return tagMapper.selectPage(ipage, queryWrapper);
	}
	
	@Override
	public SimpleResponse insert(Tag tag) {
		String userId = SecurityContext.getUserId();
		tag.setId(IdGenerateUtil.getId());
		tag.setCreateTime(new Date());
		tag.setCreateUser(userId);
		int result = tagMapper.insert(tag);
		return CommonUtils.msg(result);
	}
	
	@Override
	public SimpleResponse editById(Tag tag) {
		String userId = SecurityContext.getUserId();
		tag.setUpdateTime(new Date());
		tag.setUpdateUser(userId);
		int result = tagMapper.updateById(tag);
		return CommonUtils.msg(result);
	}
	
	
	@Override
	public SimpleResponse deleteById(String id) {
		int result = tagMapper.deleteById(id);
		return CommonUtils.msg(result);
	}

	@Override
	public SimpleResponse selectionTag(String tagId) {
		String userId = SecurityContext.getUserId();
		int result = tagMapper.selectionTag(userId, tagId);
		return CommonUtils.msg(result);
	}
	
	@Override
	public SimpleResponse selectionTags(String[] tagIds) {
		String userId = SecurityContext.getUserId();
		int result = tagMapper.deleteTagByUserId(userId);
		if(tagIds.length > 0) {
			result = tagMapper.selectionTags(userId, tagIds);
		}
		return CommonUtils.msg(result);
	}

	@Override
	public SimpleResponse cancel(String tagId) {
		String userId = SecurityContext.getUserId();
		int result = tagMapper.cancelTag(userId, tagId);
		return CommonUtils.msg(result);
	}
	
	@Override
	public SimpleResponse listTagByUserId(){
		String userId = SecurityContext.getUserId();
		List<Tag> isSelection = new ArrayList<Tag>();
		List<Tag> noSelection = new ArrayList<Tag>();
		List<Tag> tags = tagMapper.selectList(new QueryWrapper<Tag>());
		List<String> tagIds = tagMapper.listTagIdByUserId(userId);
		if(tagIds == null || tagIds.size() == 0) {
			tagIds = tagMapper.selectByNotExistSelected();
			String[] array = tagIds.stream().toArray(String[]::new);
			if(array.length > 0) {
				tagMapper.selectionTags(userId, array);
			}
		}
		for (Tag tag : tags) {
			if(tagIds.contains(tag.getId())) {
				isSelection.add(tag);
			} else {
				noSelection.add(tag);
			}
		}
		return SimpleResponse.success().put("isSelection", isSelection).put("noSelection", noSelection);
	}


}
